/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-27
*/
package com.rzico.order.service;

import com.rzico.account.service.EnterpriseDepositService;
import com.rzico.base.BaseMapper;
import com.rzico.base.CommResult;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.order.model.DistributionDetailVo;
import com.rzico.order.model.DistributionListVo;
import com.rzico.order.model.DistributionVo;
import com.rzico.order.mapper.DistributionVoMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <pre>
 * 盘点单业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class DistributionVoService extends BaseServiceImpl<DistributionVo, String> {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DistributionVoMapper distributionVoMapper;

    @Autowired
    private EnterpriseDepositService enterpriseDepositService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Override
    public BaseMapper<DistributionVo, String> getMapper() {
        return distributionVoMapper;
    }

    /**
     */
    public List<DistributionVo> selectDistributionVoList(Map<String,Object> params){
        return distributionVoMapper.selectDistributionVoList(params);
    }

    public List<DistributionDetailVo> selectDistributionDetailVo(Map<String,Object> params){
        return distributionVoMapper.selectDistributionDetailVo(params);
    }

    public List<DistributionListVo> selectDistributionListVo(Map<String,Object> params){
        return distributionVoMapper.selectDistributionListVo(params);
    }


    public Boolean checkWaitDistribution(Long enterpriseId,BigDecimal amount){
        Map<String,Object> params = new HashMap<>();
        params.clear();
        params.put("enterpriseId",enterpriseId);
        params.put("isDistribution", 0);
        List<DistributionVo> dlist = distributionVoMapper.selectDistributionVoList(params);
        BigDecimal balance = BigDecimal.ZERO;
        for (DistributionVo vo:dlist) {
            if (vo!=null) {
                balance = balance.add(vo.getDistributionAmount());
            }
        }



        boolean w = balance.compareTo(amount)>=0;
        if (w) {
            return w;
        }

        Enterprise enterprise = enterpriseService.selectByPrimaryKey(enterpriseId);
        balance = balance.add(enterprise.getBalance());
        return balance.compareTo(amount)>=0;

    }


    @Transactional(rollbackFor = Exception.class)
    public void jobStart() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date distributionDate = DateUtils.truncate(new Date(),Calendar.DATE);
        Map<String,Object> params = new HashMap<>();
        params.put("distributionDate", distributionDate);

        distributionVoMapper.distributionOrderStart(params);
        distributionVoMapper.distributionAskforStart(params);

        params.put("orderStatus", 2);

        params.put("sortType","asc");


        //结算货款
        List<DistributionVo> distributionVoList = distributionVoMapper.selectDistributionVoList(params);
        for (DistributionVo distributionVo:distributionVoList) {
            enterpriseDepositService.addBalance(distributionVo.getEnterpriseId(),
                    distributionVo.getDistributionAmount(),
                    0,sdf.format(distributionVo.getOrderDate()).concat("结算款"),null);
        }


        //自动提现
        params.put("hasBalance", true);
        List<Enterprise> enterpriseList = enterpriseService.selectList(params);
        for (Enterprise enterprise:enterpriseList) {
            enterpriseDepositService.decBalance(enterprise.getId(),
                    enterprise.getBalance(),
                    2,sdf.format(distributionDate).concat("自动提现"),null);
        }

    }

}

